package vn.huync.smsbackupandrestore.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import vn.huync.smsbackupandrestore.others.Utils;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

public class SmsModel implements Serializable, Parcelable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int protocol;
	String address;
	long date;			//date in millisecond
	int type;
	String subject;
	String body;
	String serviceCenter;
	int read;
	int status;
	int lock;
	
	public SmsModel(int protocol, String address, long date, int type,
			String subject, String body, String serviceCenter, int read,
			int status, int lock) {
		super();
		this.protocol = protocol;
		
		if(address != null)
			this.address = address;
		else this.address = "null";
		
		this.date = date;
		
		this.type = type;
		
		if(subject != null)
			this.subject = subject;
		else this.subject = "null";
		
		if(body != null)
			this.body = body;
		else this.body = "null";
		
		if(serviceCenter != null)
			this.serviceCenter = serviceCenter;
		else this.serviceCenter = "null";
		
		this.read = read;
		this.status = status;
		this.lock = lock;
	}
	
	public SmsModel(Parcel source){
		protocol = source.readInt();
		address = source.readString();
		date = source.readLong();			//date in millisecond
		type = source.readInt();
		subject = source.readString();
		body = source.readString();
		serviceCenter = source.readString();
		read = source.readInt();
		status = source.readInt();
		lock = source.readInt();
	}

	public int getProtocol() {
		return protocol;
	}

	public String getAddress() {
		return address;
	}

	public long getDate() {
		return date;
	}

	public int getType() {
		return type;
	}

	public String getSubject() {
		return subject;
	}

	public String getBody() {
		return body;
	}

	public String getServiceCenter() {
		return serviceCenter;
	}

	public int getRead() {
		return read;
	}

	public int getStatus() {
		return status;
	}

	public int getLock() {
		return lock;
	}
	
	public static void addListToList(ArrayList<SmsModel> sumList, ArrayList<SmsModel> itemList){
		if(sumList == null || itemList == null)
			return;
		for(SmsModel sms:itemList){
			if(Utils.IsExistSmsInList(sms.getDate(), sms.getAddress(), sumList))
				continue;
			else sumList.add(sms);
		}
	}
	
	public static void addListToList(ArrayList<SmsModel> sumList, ArrayList<SmsModel> itemList, long from, long to){
		if(sumList == null || itemList == null)
			return;
		for(SmsModel sms:itemList){
			if(Utils.IsExistSmsInList(sms.getDate(), sms.getAddress(), sumList))
				continue;
			else{
				long date = sms.getDate();
				if((from == 0 && to == 0) || (date >= from && date <= to))
					sumList.add(sms);
			}
		}
	}
	
	public static void deleteSmsExistInDbFromArray(Context context, ArrayList<SmsModel> smsList){
		ArrayList<SmsModel> lSmsInDatabase = new ArrayList<SmsModel>();
		Utils.ReadSMSFromDbToList(context, lSmsInDatabase);
		
		Iterator<SmsModel> iterator = smsList.iterator();
		while (iterator.hasNext()) {
			SmsModel sms = iterator.next();
			if(Utils.IsExistSmsInList(sms.getDate(), sms.getAddress(), lSmsInDatabase))
				iterator.remove();
		}
	}

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(protocol);
		dest.writeString(address);
		dest.writeLong(date);
		dest.writeInt(type);
		dest.writeString(subject);
		dest.writeString(body);
		dest.writeString(serviceCenter);
		dest.writeInt(read);
		dest.writeInt(status);
		dest.writeInt(lock);
	}
	
	public static final Parcelable.Creator<SmsModel> CREATOR = new Parcelable.Creator<SmsModel>() {

		public SmsModel createFromParcel(Parcel source) {
			return new SmsModel(source);
		}

		public SmsModel[] newArray(int size) {
			return new SmsModel[size];
		}
		
	};
}
